package com.ah.hf.controller;

import com.ah.hf.common.R;
import com.ah.hf.entity.Employee;
import com.ah.hf.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    //登录请求
     @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//         加密
         String password = employee.getPassword();
         password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查数据库
         LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
         qw.eq(Employee::getUsername , employee.getUsername());
         Employee emp = employeeService.getOne(qw);

         //查询用户名
         if (emp == null){
              return R.error("用户名错误");
         }
         //比对密码
         if (!emp.getPassword().equals(password)){
             return R.error("密码不正确");
         }
         //
         if (emp.getStatus() != 1){
             return R.error("登录转态异常，请联系管理员" );
         }
         request.getSession().setAttribute("employee",emp.getId());

         //登录成功
         return R.success(emp);
     }

     //退出登录
     @PostMapping("/logout")
     public R<String> logout(HttpServletRequest request){
         //清理用户session
         request.getSession().removeAttribute("employee");
         return R.success("退出成功");
     }

    //新增员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
         log.info("新增员工");
         //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //获取当前登录用户的id
        Long id = (Long) request.getSession().getAttribute("employee");

        //创建人和更新人的id
        /*employee.setCreateUser(id);
        employee.setUpdateUser(id);

        //创建时间和更新时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        //调用service层，service实现了mybatisPlus接口

        boolean save = employeeService.save(employee);
        return R.success("新增员工成功");
     }

     //分页查询
     @GetMapping("/page")
     public R<Page> page(int page , int pageSize ,String name){
         log.info("page = {} ,pageSize = {} , name = {} " , page,pageSize,name);
         //分页查询构造器
         Page pageInfo = new Page(page,pageSize);

         LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();

         //like条件查询name
         qw.like(StringUtils.isNotEmpty(name) , Employee::getName , name);

         //按照更新时间进行排序
         qw.orderByDesc(Employee::getUpdateTime);

         //调用service层，进行分页查询操作
         employeeService.page(pageInfo,qw);

         //返回数据
         return R.success(pageInfo);
     }
     @PutMapping
     public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
         log.info(employee.toString());

         //获取修改人的id
         Long id = (Long) request.getSession().getAttribute("employee");

         //设置修改的人的id
         /*employee.setUpdateUser(id);

         //修改最后一次修改的日期
         employee.setUpdateTime(LocalDateTime.now());*/

         //调用service层修改 setUpdateUser  setUpdateTime status
         employeeService.updateById(employee);
         return R.success("员工转态修改成功");
     }

     //根据路径的变量来接受请求
    //根据id查询员工
     @GetMapping("/{id}")
     public R<Employee> getByID(
             //接受路径变量
             @PathVariable Long id){
         //查询结果返回，前端页面做回显函数
         Employee employee = employeeService.getById(id);
         if (employee != null){
             return R.success(employee);
         }
         return R.error("没有查询到该员工");
     }


}
